package com.example.Sao.Tools;

import android.annotation.TargetApi;
import android.util.Base64;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import io.objectbox.annotation.Transient;

/**
 * @ProjectName: scan
 * @Package: com.example.Sao.Tools
 * @ClassName: DesECBUtil
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/5/26 22:11
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/5/26 22:11
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class DesECBUtil {
    public static final String PASSWORD_ENC_SECRET = "mythmayor";

    @TargetApi(26)
    public static String encryptDES(byte[]encryptString, String encryptKey)
    {
        String encryptword = "";
        try {
            //使用指定密钥构造IV
            IvParameterSpec iv = new IvParameterSpec(encryptKey.getBytes("US-ASCII"));
            //根据给定的字节数组和指定算法构造一个密钥。
            SecretKeySpec skeySpec = new SecretKeySpec(encryptKey.getBytes("US-ASCII"), "DES");
            //返回实现指定转换的 Cipher 对象
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            //加密初始化
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            //加密同时解码成字符串返回
            encryptword  = Base64.encodeToString(cipher.doFinal(encryptString),Base64.DEFAULT);
            Log.d("ECB","加密字符："+encryptword);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (NoSuchPaddingException e){
            e.printStackTrace();
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
        }catch (InvalidKeyException e){
            e.printStackTrace();
        }catch (InvalidAlgorithmParameterException e){
            e.printStackTrace();
        }catch (IllegalBlockSizeException e){
            e.printStackTrace();
        }catch (BadPaddingException e){
            e.printStackTrace();
        }
        return encryptword;
    }

}
